#include "service.h"
#include <QTextStream>
#include <QFile>
#include <QDebug>
#include <Ari/operators.h>
namespace Ser {

Service::Service()
{

}

Service* Service::Instance()
{
    static Service m_instance;
    return &m_instance;
}

QVector<QString> Service::Open_Deploy_File(QString path)
{
    QVector<QString> v;
    // QString file_Path="D:\\QT_Project\\MyTest\\mode\\template_1.txt";
    QString file_Path=path;
    //qDebug()<<file_Path<<v.size();
    QFile file(file_Path);
    if(!file.open(QIODevice::ReadOnly | QIODevice::Text))//打开文件
    {
        qDebug() << "Can't open the file!";
        return v;
    }
    QTextStream in(&file);  //用文件构造流
    QString line = in.readLine();//读取一行放到字符串里
    while(!line.isNull())//字符串有内容
    {
        //qDebug()<<"转换前"<<line;
        line=line.toLower();
        line=line.replace(";"," ");
        line=line.replace("\\","//");
        line=line.remove(QRegExp("\\s"));
        line=line.section("//",0,0);
        //qDebug()<<"转换后"<<line;
        if(!line.isEmpty()){
            v.push_back(line);
        }
        line=in.readLine();//循环读取下行
    }
    return v;
}

QString Service::getFindText(QVector<QString> v, QString txtName)
{
    QString findTxt="";
    for(int i=0;i<v.size();i++){
        if(v[i].section("=",0,0)==txtName){
            return findTxt=v[i].section("=",1,1);
        }
    }
    return findTxt;
}

void Service::Deploy_ARI_ErrorRate(QString modeID)
{
    QString path=QString("D:\\QT_Project\\MyTest\\mode\\template_%1.txt").arg(modeID);
    QString path_ARI=QString("D:\\QT_Project\\MyTest\\mode\\template_%1\\arithmetic.txt").arg(modeID);
    QVector<QString> v=Open_Deploy_File(path);
    ari_Vector=Open_Deploy_File(path_ARI);
    for(int i=0;i<ari_Vector.size();i++){
        qDebug()<<ari_Vector[i];
    }

    for(int i=0;i<v.size();i++){
        //qDebug()<<v[i];
        if(v[i].section("=",0,0)=="modeFile"){
             location_Error.modeFile=getFindText(v,"modeFile");
        }
        else if(v[i].section("=",0,0)=="mode"){
             location_Error.mode=getFindText(v,"mode");
        }
        else if(v[i].section("=",0,0)=="length"){
             location_Error.length=getFindText(v,"length").toDouble();
        }
        else if(v[i].section("=",0,0)=="width"){
             location_Error.width=getFindText(v,"width").toDouble();
        }
        else if(v[i].section("=",0,0)=="angle"){
             location_Error.angle=getFindText(v,"angle").toDouble();
        }
        else if(v[i].section("=",0,0)=="angle_error"){
             location_Error.angle_error=QString_trun_Double(getFindText(v,"angle_error"));
        }
        else if(v[i].section("=",0,0)=="length_error"){
             location_Error.length_error=QString_trun_Double(getFindText(v,"length_error"));
        }
        else if(v[i].section("=",0,0)=="width_error"){
             location_Error.width_error=QString_trun_Double(getFindText(v,"width_error"));
        }
        else if(v[i].section("=",0,0)=="area_error"){
              location_Error.area_error=QString_trun_Double(getFindText(v,"area_error"));
        }
        else if(v[i].section("=",0,0)=="image_error"){
              location_Error.image_error=QString_trun_Double(getFindText(v,"image_error"));
        }
        else if(v[i].section("=",0,0)=="diameter_error"){
              location_Error.diameter_error=QString_trun_Double(getFindText(v,"diameter_error"));
        }
    }
//    qDebug()<<"modeFile"<<location_Error.modeFile;
//    qDebug()<<"mode"<<location_Error.mode;
//    qDebug()<<"length"<<location_Error.length;
//    qDebug()<<"width"<<location_Error.width;
//    qDebug()<<"angle"<<location_Error.angle;
//    qDebug()<<"length_error"<<location_Error.length_error;
//    qDebug()<<"width_error"<<location_Error.width_error;
//    qDebug()<<"angle_error"<<location_Error.angle_error;
//    qDebug()<<"area_error"<<location_Error.area_error;
//    qDebug()<<"image_error"<<location_Error.image_error;
//    qDebug()<<"diameter_error"<<location_Error.diameter_error;
}

double Service::QString_trun_Double(QString str)
{
    double s=str.section("%",0,0).toDouble();
    s/=100;
    return s;
}

QString Service::Extract_variable(QString str,QString start_Symbol,QString end_Symbol)
{
    if(str.isEmpty()||str.isNull())return "";
    QString s;
    int start=str.indexOf(start_Symbol)+1;
    int end=str.indexOf(end_Symbol)-1;
    s=str.mid(start,end);
    return s;
}

Coords Service::Excute_ARI()
{
    bool isExcute=true;
    Coords coords;
    if(ari_Vector.size()==0){
        return coords;
    }
    coords.isNull=false;
    for(int i=0;i<ari_Vector.size();i++){
        //qDebug()<<ari_Vector[i];
        if(ari_Vector[i].startsWith("threshold")){
            if(!isExcute)break;
            isExcute=Ope::Operators::Threshold(ari_Vector[i]);
        }
        else if(ari_Vector[i].startsWith("connection")){
            qDebug()<<";;;;;";
            if(!isExcute)break;
            isExcute=Ope::Operators::Connection(ari_Vector[i]);
        }
        else if(ari_Vector[i].startsWith("opening_circle")){
            if(!isExcute)break;
            isExcute=Ope::Operators::Opening_Circle(ari_Vector[i]);
        }
        else if(ari_Vector[i].startsWith("fill_up")){
            if(!isExcute)break;
            isExcute=Ope::Operators::Fill_Up(ari_Vector[i]);
        }
        else if(ari_Vector[i].startsWith("shape_trans")){
            if(!isExcute)break;
            isExcute=Ope::Operators::Shape_Trans(ari_Vector[i]);
        }
        else if(ari_Vector[i].startsWith("reduce_domain")){
            if(!isExcute)break;
            isExcute=Ope::Operators::Reduce_Domain(ari_Vector[i]);
        }
        else if(ari_Vector[i].startsWith("select_shape")){
            if(!isExcute)break;
            isExcute=Ope::Operators::Select_Shape(ari_Vector[i]);
        }        
        else if(ari_Vector[i].startsWith("smallest_rectangle2")){
            if(!isExcute)break;
            isExcute=Ope::Operators::Smallest_Rectangle2(ari_Vector[i]);
        }
        else if(ari_Vector[i].startsWith("gen_rectangle2")){
            if(!isExcute)break;
            isExcute=Ope::Operators::Gen_Rectangle2(ari_Vector[i]);
        }
        else if(ari_Vector[i].startsWith("area_center")){
            if(!isExcute)break;
            isExcute=Ope::Operators::AreaCenter(ari_Vector[i]);

        }
    }
}



bool Service::find_HObject(QString s,HalconCpp::HObject *image)
{
    auto it=image_Map.begin();
    HalconCpp::HObject ima;
    if(!image_Map.contains(s)){
        return false;
    }
    while(it!=image_Map.end()){
        if(it.key()==s){
            ima=it.value();
            *image=ima;
            break;
        }
        it++;
    }
    return true;
}

bool Service::find_HTuple(QString s, HTuple *tuple)
{
    auto it=parameter_Map.begin();
    if(!parameter_Map.contains(s)){
        return false;
    }
    while(it!=parameter_Map.end()){
        if(it.key()==s){
            *tuple=it.value();
            break;
        }
        it++;
    }
    return true;
}

bool Service::readImage()
{
    HObject image;
    HalconCpp::ReadImage(&image,"C:/Users/Windows10/Desktop/wafer/Image_20210316155921463.bmp");
    try{
        image_Map.insert("image",image);
    }catch (HalconCpp::HException e){
        return false;
    }   
    //执行算法
    std::lock_guard<std::mutex> lk(m);
    Excute_ARI();
    return true;
}



}
